package com.example.demogson;

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView show;
    private String annimalStr = "{\n" +
            "\t\"animals\":{\n" +
            "\t\"dog\":[\n" +
            "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n" +
            "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n" +
            "\t],\n" +
            "\t\"cat\":{\"name\":\"Matilda\"}\n" +
            "}\n" +
            "}";
    private String weatherStr = "{\"time\":\"2019-03-25 09:00:00\",\"cityInfo\":{\"city\":\"广州市\",\"cityId\":\"101280101\",\"parent\":\"广东\",\"updateTime\":\"08:39\"},\"date\":\"20190325\",\"message\":\"Success !\",\"status\":200,\"data\":{\"shidu\":\"99%\",\"pm25\":5.0,\"pm10\":6.0,\"quality\":\"优\",\"wendu\":\"13\",\"ganmao\":\"各类人群可自由活动\",\"yesterday\":{\"date\":\"24\",\"sunrise\":\"06:29\",\"high\":\"高温 17.0℃\",\"low\":\"低温 12.0℃\",\"sunset\":\"18:39\",\"aqi\":15.0,\"ymd\":\"2019-03-24\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},\"forecast\":[{\"date\":\"25\",\"sunrise\":\"06:28\",\"high\":\"高温 19.0℃\",\"low\":\"低温 14.0℃\",\"sunset\":\"18:40\",\"aqi\":18.0,\"ymd\":\"2019-03-25\",\"week\":\"星期一\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"26\",\"sunrise\":\"06:27\",\"high\":\"高温 24.0℃\",\"low\":\"低温 16.0℃\",\"sunset\":\"18:40\",\"aqi\":69.0,\"ymd\":\"2019-03-26\",\"week\":\"星期二\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"27\",\"sunrise\":\"06:26\",\"high\":\"高温 26.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":52.0,\"ymd\":\"2019-03-27\",\"week\":\"星期三\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"28\",\"sunrise\":\"06:25\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":55.0,\"ymd\":\"2019-03-28\",\"week\":\"星期四\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"29\",\"sunrise\":\"06:24\",\"high\":\"高温 25.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:41\",\"aqi\":52.0,\"ymd\":\"2019-03-29\",\"week\":\"星期五\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"30\",\"sunrise\":\"06:23\",\"high\":\"高温 25.0℃\",\"low\":\"低温 19.0℃\",\"sunset\":\"18:42\",\"aqi\":57.0,\"ymd\":\"2019-03-30\",\"week\":\"星期六\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"31\",\"sunrise\":\"06:22\",\"high\":\"高温 24.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-03-31\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"01\",\"sunrise\":\"06:21\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-04-01\",\"week\":\"星期一\",\"fx\":\"东北风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"02\",\"sunrise\":\"06:20\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-02\",\"week\":\"星期二\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"晴\",\"notice\":\"愿你拥有比阳光明媚的心情\"},{\"date\":\"03\",\"sunrise\":\"06:19\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-03\",\"week\":\"星期三\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"04\",\"sunrise\":\"06:18\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-04\",\"week\":\"星期四\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"05\",\"sunrise\":\"06:18\",\"high\":\"高温 31.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-05\",\"week\":\"星期五\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"06\",\"sunrise\":\"06:17\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-06\",\"week\":\"星期六\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"07\",\"sunrise\":\"06:16\",\"high\":\"高温 29.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-07\",\"week\":\"星期日\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"08\",\"sunrise\":\"06:15\",\"high\":\"高温 29.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-08\",\"week\":\"星期一\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"}]}}";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = findViewById(R.id.show_view);
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                User user = new User("j.cole",35);
                String jsonString = gson.toJson(user);
                Log.e(TAG,"jsonString"+jsonString);
                try {
                    JSONObject jsonObjectUser = new JSONObject(jsonString);
                    Log.e(TAG,"jsonObjectUser="+jsonObjectUser);
                    show.setText("jsonObjectUser="+jsonObjectUser);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                User user = new User("j.cole",35);
                String jsonString = gson.toJson(user);

                User user1 = gson.fromJson(jsonString,User.class);
                show.setText("user="+user1.toString());
                Log.e(TAG,"user="+user1.toString());
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                JsonRootBean rootBean = gson.fromJson(annimalStr,JsonRootBean.class);
                String catName = rootBean.getAnimals().getCat().getName();
                Log.e(TAG,"catName=  "+catName);
                show.setText("catName=  "+catName);

            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void  onClick(View v) {
                 Gson gson = new Gson();
                 Weather weather = gson.fromJson(weatherStr,Weather.class);
                 String type =  weather.getData().getForecast().get(0).getType();
                 Log.e(TAG,"广州今天的天气类型是 type="+type);
                 show.setText("广州今天的天气类型是 type="+type);
            }
        });
    }
}
